package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.enumerate.UploadResult;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 回读接收卡连接关系文件上报事件
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetReceiverMappingFileUpload implements CompleteEntityEvent {

    /**
     * 上报地址
     */
    private String url;

    /**
     * 接收卡连接关系文件是否回读成功
     */
    private Boolean loadSuccess;

    /**
     * 文件上传结果，没有回读成功时为 null
     */
    private UploadResult uploadResult;

    @Override
    public GetReceiverMappingFileUpload byteConvertObject(ByteString bytes) {
        CompleteEvent.GetReceiverMappingFileUpload getReceiverMappingFileUpload = null;
        try {
            getReceiverMappingFileUpload = CompleteEvent.GetReceiverMappingFileUpload.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(getReceiverMappingFileUpload);
        this.setUrl(getReceiverMappingFileUpload.getUrl());
        this.setLoadSuccess(getReceiverMappingFileUpload.getLoadSuccess());
        this.setUploadResult(UploadResult.getUploadResultStruct(getReceiverMappingFileUpload.getUploadResult()));
        return this;
    }
}

